package xin.marcher.module.common.utils;

/**
 *
 */
public class HashUtil {

    private static final int MURMURHASH_M = 0x5bd1e995;

    public static long murMurHash(byte[] key) {
        int len = key.length;
        int h = 97 ^ len;
        int index = 0;

        while (len >= 4) {
            int k = (key[index] & 0xff) | ((key[index + 1] << 8) & 0xff00)
                    | ((key[index + 2] << 16) & 0xff0000)
                    | (key[index + 3] << 24);

            k *= MURMURHASH_M;
            k ^= (k >>> 24);
            k *= MURMURHASH_M;
            h *= MURMURHASH_M;
            h ^= k;
            index += 4;
            len -= 4;
        }

        switch (len) {
            case 3:
                h ^= (key[index + 2] << 16);

            case 2:
                h ^= (key[index + 1] << 8);

            case 1:
                h ^= key[index];
                h *= MURMURHASH_M;
        }

        h ^= (h >>> 13);
        h *= MURMURHASH_M;
        h ^= (h >>> 15);
        return ((long) h & 0xffffffffL);
    }
}
